import { _decorator, Component, find, Node, resources, Sprite, SpriteFrame } from 'cc';
import { MJStyleEnum } from '../script/Enum';
import GameController from '../script/GameController';
const { ccclass, property } = _decorator;

@ccclass('MJ')
export class MJ extends Component {

    private _mj: number = -1;
    private spMJ: Node = null;

    private _style;
    private _deckIndex;
    private _line;

    protected onLoad(): void {
        this.spMJ = find("spMJ", this.node);

        this.node.on(Node.EventType.TOUCH_START, this.onTouchStart, this);
        this.node.on(Node.EventType.TOUCH_MOVE, this.onTouchMove, this);
        this.node.on(Node.EventType.TOUCH_END, this.onTouchEnd, this);
        this.node.on(Node.EventType.TOUCH_CANCEL, this.onTouchEnd, this);
    }

    start() {

    }

    update(deltaTime: number) {

    }

    private onTouchStart() {
        if (this.isInHand()) {

        } else {
            if (this.getMJIndex() == this._line.length - 1) {
                GameController.getInstance().inHand(this._deckIndex)
            }
        }

       
    }

    private onTouchMove() {

    }


    private onTouchEnd() {

    }

    private onTouchCancel() {

    }

    public set Style(value) {
        this._style = value;
    }

    public get MJValue() {
        return this._mj;
    }

    public set MJValue(value: number) {
        this._mj = value;
        resources.load("mj/" + this._style + "_" + value + "/spriteFrame", SpriteFrame, (err, spriteFrame) => {
            if (err) {
                console.log(err);
                return;
            }
            this.spMJ.getComponent(Sprite).spriteFrame = spriteFrame;
        })
    }

    public isInHand() {
        return this._style == MJStyleEnum.STYLE_STAND ? true : false;
    }

    public getDeckIndex() {
        return this._deckIndex;
    }

    public set Line(value) {
        this._line = value;
    }

    public getMJIndex() {
        console.log(this._line.indexOf(this.node));
        return this._line.indexOf(this.node);
    }

    public setIndex(deck) {
        this._deckIndex = deck;
    }

    public onChiEffect() {
        this.node.active = false;
    }
}

